<%- partial('../sidebar') %>

<div id='content'>
  <div class='panel'>
    <div class='header'>
      <ul class='breadcrumb'>
        <li><a href='/'>主页</a><span class='divider'>/</span></li>
        <li class='active'>新建通知</li>
      </ul>
    </div>
    <div class='inner'>
      <% if(typeof(edit_error) !== 'undefined' && edit_error){ %>
      <div class="alert alert-error">
        <a class="close" data-dismiss="alert" href="#">&times;</a>
        <strong><%= edit_error %></strong>
      </div>
      <% } %>
      <% if (typeof(error) !== 'undefined' && error) { %>
      <div class="alert alert-error">
        <a class="close" data-dismiss="alert" href="#">&times;</a>
        <strong><%= error %></strong>
      </div>
      <% } %>
      <% if (typeof(success) !== 'undefined' && success) { %>
      <div class="alert alert-success">
        <strong><%= success %></strong>
      </div>
      <script>
      setTimeout("window.location.href = '/notificationmanager/create';", 2000);
      </script>
      <% } else { %>
      <form id='signup_form' class='form-horizontal' action='/notificationmanager/create' method='post'>
        <div class='control-group'>
          <label class='control-label' for='title'>标题</label>

          <div class='controls'>
            <% if (typeof(title) !== 'undefined') { %>
            <input class='input-xlarge' id='title' name='title' size='30' type='text' value='<%= title %>'/>
            <% } else { %>
            <input class='input-xlarge' id='title' name='title' size='30' type='text' value=''/>
            <% } %>
          </div>
        </div>
        <div class='control-group'>
          <label class='control-label' for='department'>部门</label>

          <div class='controls'>
            <% if (typeof(departments) !== 'undefined') { %>
            <select class="form-control" name="department" id="department">
              <%-partial('../share/option',{collection:departments, as:'opt', val:''}) %>
            </select>
            <% } %>
          </div>
        </div>
        <div class='control-group'>
          <label class='control-label' for='users'>人员</label>

          <div class='controls' id="users_wrap">
            <br />
          </div>
          <input type="hidden" name="dep_users_count" id="dep_users_count" value="0" />
        </div>
        <div class='control-group'>
          <label class='control-label' for='content'>通知内容</label>
          <div class='controls'>
            <textarea id='content' class="editor" name='content'><% if (typeof(content) !== 'undefined') { %><%= content %><% } %></textarea>
          </div>
        </div>

        <input type='hidden' name='_csrf' value='<%= csrf %>'/>

        <div class='form-actions'>
          <input type='submit' class='span-primary' value='创建'/>
        </div>
      </form>
      <% } %>
    </div>
  </div>
</div>
<!-- markdown editor -->
<%- Loader('/public/editor.min.js')
.js('/public/libs/editor/editor.js')
.js('/public/libs/webuploader/webuploader.withoutimage.js')
.js('/public/libs/editor/ext.js')
.js('/public/cron/js/index.js')
.css('/public/cron/css/theme.css')
.done(assets, config.site_static_host, config.mini_assets)
%>
<script>
jQuery(function($){
  var editor = new Editor();
  editor.render($('.editor')[0]);

  $('#department').change(function(){
    update_user_list();
  });
  function update_user_list(){
    var dep = $('#department').val();
    $.getJSON("/notificationmanager/userlist/" + dep, function(json){
      if( typeof(json.success) == 'undefined' ){
        $('#users_wrap').html( '无法获得部门用户' );
        return;
      }
      var users_html = '';
      var users = json.users;
      if( users.length === 0 ){
        users_html = '该部门暂无人员配置';
      }
      $('#dep_users_count').val( users.length );
      for( i in users ){
        if ( i === 'remove' ){
          continue;
        }
        users_html += '<label class="checkbox inline"><input type="checkbox" name="users[]" value="' + users[i] + '"> '+ users[i] + ' </label>';
      }
      users_html += '<br />';
      $('#users_wrap').html( users_html );
    });


  }
  update_user_list();

});
</script>